package cn.nohan.funplay.module.member.mongo.mapper;

import cn.nohan.funplay.module.member.mongo.entity.MongoUserTrajectoryEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import java.util.List;

/**
 * @author caozhipeng
 * @date 2025/8/1
 */
public interface LocationRepository extends MongoRepository<MongoUserTrajectoryEntity, Long> {


    // 1. 查找靠近某个点的文档 (使用 $nearSphere)
    List<MongoUserTrajectoryEntity> findByLocationNear(Point point, Distance distance);


    Page<MongoUserTrajectoryEntity> findByLocationNear(Point point, Distance distance, Pageable pageable);

    Page<MongoUserTrajectoryEntity> findByLocationNearAndUserIdNot(Point point, Distance distance, Long userId, Pageable pageable);


    // 新增方法：返回包含距离的结果
    @Query(value = "{ 'location' : { $nearSphere : { $geometry : ?0 }, $maxDistance : ?1 } }",
            fields = "{ 'distanceField' : { $meta: 'geoDistance' } }")
    List<MongoUserTrajectoryEntity> findNearWithDistance(org.bson.Document location, double maxDistance);
}
